package com.shengqi.xinnongren.work.ui;

import android.content.Intent;
import android.media.MediaPlayer;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.VideoView;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.OnClick;

import com.bumptech.glide.Glide;
import com.shengqi.xinnongren.R;
import com.shengqi.xinnongren.common.base.BaseMvpActivity;
import com.shengqi.xinnongren.common.config.user.UserManager;
import com.shengqi.xinnongren.common.utils.LogUtil;
import com.shengqi.xinnongren.common.utils.SPUtils;
import com.shengqi.xinnongren.common.utils.ToastHelper;
import com.shengqi.xinnongren.common.widgets.CountdownCircleView;
import com.shengqi.xinnongren.work.model.HttpRespond;
import com.shengqi.xinnongren.work.ui.home.activities.ChooseStoreActivity;
import com.shengqi.xinnongren.work.ui.home.activities.MainActivity;
import com.shengqi.xinnongren.work.ui.home.model.SplashBean;


public class SplashActivity extends BaseMvpActivity<SplashView, SplashPresenter> implements SplashView, MediaPlayer.OnCompletionListener, MediaPlayer.OnPreparedListener, MediaPlayer.OnErrorListener {

    @BindView(R.id.ccv_count_down)
    CountdownCircleView mCircleView;

    @BindView(R.id.vv_splash)
    VideoView mVideoView;

    @BindView(R.id.iv_splash)
    ImageView mIvSplash;

    @BindView(R.id.tv_seconds)
    TextView mTvSeconds;

    //页面跳转延迟时长
    private static int VALUE_DELAYED_TIME = 3;

    @Override
    protected int setContentLayoutRes() {
        return R.layout.activity_splash;
    }

    @Override
    protected SplashPresenter createPresenter() {
        return new SplashPresenter();
    }

    @Override
    protected void initView() {

        /*if (((int) SPUtils.get(this, "splash_type", 2)) == 0) {
            VALUE_DELAYED_TIME = (int) SPUtils.get(this, "splash_second", 3);
            mVideoView.setVisibility(View.GONE);
            mIvSplash.setVisibility(View.VISIBLE);
            mIvSplash.setImageURI(Uri.parse((String) SPUtils.get(this, "splash_path", "")));
        }

        if (((int) SPUtils.get(this, "splash_type", 2)) == 1) {
            mIvSplash.setVisibility(View.GONE);
            mVideoView.setVisibility(View.VISIBLE);
            mVideoView.setVideoPath((String) SPUtils.get(this, "splash_path", ""));
            mVideoView.setOnCompletionListener(this);
            mVideoView.setOnPreparedListener(this);
            mVideoView.setOnErrorListener(this);
            mVideoView.start();
        }*/
        mCircleView.start(VALUE_DELAYED_TIME, () -> startMainActivity());
    }

    @Override
    protected void initData() {
        //mPresenter.getSplashContent();
    }

    /**
     * 启动主页面
     */
    @OnClick(R.id.ccv_count_down)
    void startMainActivity() {
        if (mVideoView.isPlaying()) {
            mVideoView.pause();
        }
        mCircleView.stopCircleAnimation();
        startActivity(new Intent(this, MainActivity.class));
        finish();
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        startMainActivity();
    }

    @Override
    public void onPrepared(MediaPlayer mp) {
        int duration = mp.getDuration() / 1000;
        mCircleView.start(VALUE_DELAYED_TIME, this::startMainActivity);
        mTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(() -> {
                    VALUE_DELAYED_TIME--;
                    mTvSeconds.setText("" + VALUE_DELAYED_TIME);
                    if (VALUE_DELAYED_TIME < 0) {
                        mTimer.cancel();
                    }
                });
            }
        }, 1000, 1000);
    }

    @Override
    public boolean onError(MediaPlayer mp, int what, int extra) {
        mCircleView.start(VALUE_DELAYED_TIME, this::startMainActivity);
        return true;
    }

    Timer mTimer = new Timer();

    @Override
    public void showSplashContent(HttpRespond<SplashBean> respond) {
        if (respond.result == 1) {
            SplashBean bean = respond.data;

            //图片
            VALUE_DELAYED_TIME = bean.getSecond();
            if (bean.getType() == 0) {
                mVideoView.setVisibility(View.GONE);
                mIvSplash.setVisibility(View.VISIBLE);
                Glide.with(this).load(bean.getUrl()).into(mIvSplash);
                mCircleView.start(VALUE_DELAYED_TIME, this::startMainActivity);
                mTimer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        runOnUiThread(() -> {
                            VALUE_DELAYED_TIME--;
                            mTvSeconds.setText("" + VALUE_DELAYED_TIME);
                            if (VALUE_DELAYED_TIME < 0) {
                                mTimer.cancel();
                            }
                        });
                    }
                }, 1000, 1000);
            }
            //视频  暂时注释掉
            /*if (bean.getType() == 1) {
                String url = bean.getUrl().substring(bean.getUrl().lastIndexOf("/") + 1);
                String localUrl = (String) SPUtils.get(this, "splash_path", "");
                if (url.equals(localUrl.substring(localUrl.lastIndexOf("/") + 1))) {
                    mIvSplash.setVisibility(View.GONE);
                    mVideoView.setVisibility(View.VISIBLE);
                    mVideoView.setVideoPath((String) SPUtils.get(this, "splash_path", ""));
                    mVideoView.setOnCompletionListener(this);
                    mVideoView.setOnPreparedListener(this);
                    mVideoView.setOnErrorListener(this);
                    mVideoView.start();
                } else {
                    mIvSplash.setVisibility(View.GONE);
                    mVideoView.setVisibility(View.VISIBLE);
                    mVideoView.setVideoPath(bean.getUrl());
                    mVideoView.setOnCompletionListener(this);
                    mVideoView.setOnPreparedListener(this);
                    mVideoView.setOnErrorListener(this);
                    mVideoView.start();
                }
            }*/
//            DownLoadHelper.downLoad(this, bean.getUrl(), false, bean.getType());
        } else
            ToastHelper.getInstance(this).showError(respond.message);

        LogUtil.e("daf");
    }

    @Override
    public void showLoadingView() {

    }

    @Override
    public void hideLoadingView() {

    }

    @Override
    public void onNetworkError() {

    }
}
